﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.Models;
using WineStore.DAL;
using System.Data;
using System.Web.Security;

namespace WineStore.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOn model)
        {
            if (ModelState.IsValid)
            {
                if (ValidateUser(model.Email, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.Email,false);
                        return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "The email or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }


        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Customer customer)
        {
            try
            {
                // Validate in server
                if (ModelState.IsValid)
                {
                    // Luu customer vao database 
                    using (var db = new WineDbContext())
                    {
                        customer.CusPassword = EncodePassword(customer.CusPassword);
                        db.Entry(customer).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("Index", "Home");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View();
        }

        //Method to hash parameters to generate the Reset URL
        public static string EncodePassword(string password)
        {

            byte[] bytesofLink = System.Text.Encoding.UTF8.GetBytes(password);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            string HashParams = BitConverter.ToString(md5.ComputeHash(bytesofLink));

            return HashParams;
        }

        public static bool ValidateUser(string email, string password)
        {
            string encodePassword = EncodePassword(password);
            using (var db = new WineDbContext())
            {
                foreach (Customer customer in db.Customers)
                {
                    if (customer.CusEmail.Equals(email) && customer.CusPassword.Equals(encodePassword))
                        return true;
                }
            }
            return false;
        }

        // GET: /LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}
